<!DOCTYPE html>
<html>
<head>
    <title>吉垚物业后台管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" th:href="./images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- 引入css -->
    <link rel="stylesheet" href="./css/plugins/font-awesome-4.7.0/css/font-awesome.min.css"  media="all">
    <link rel="stylesheet" href="./lib/layui-v2.3.0/css/layui.css"  media="all">
    <link rel="stylesheet" href="./css/main.css"  media="all">
</head>
<body class="layui-layout-login">
<div class="login-bg">
    <div class="cover"><h1 style="color:#FBFBFB;" align="center">吉垚物业后台管理系统</h1></div>
</div>
<div class="login-content">
    <h1 class="login-box-title"><i class="fa fa-fw fa-user"></i>登录</h1>
        <div class="layui-form-item" id="unameD">
            <label class="layui-icon layui-icon-username" for="name"></label>
            <input class="layui-input" type="text" name="name" id="name" placeholder="用户名">
            <div id="unameW"></div>
        </div>
        <div class="layui-form-item" id="upassD">
            <label class="layui-icon layui-icon-password" for="password"></label>
            <input class="layui-input" type="password" name="password" id="password" placeholder="密码">
            <div id="upassW"></div>
        </div>
        <div class="layui-form-item">
            <input type="checkbox" name="rememberMe" title="记住我" lay-skin="primary"><div class="layui-unselect layui-form-checkbox" lay-skin="primary"><span>记住我</span><i class="layui-icon layui-icon-ok"></i></div>
            <a class="layui-layout-right forget-password" href="javascript:alert('请联系超级管理员！')">忘记密码?</a>
        </div>
        <button class="layui-btn layui-btn-fluid" onclick="login()"><i class="fa fa-sign-in fa-lg fa-fw"></i> 登录
        </button>
</div>

<script src="./lib/layui-v2.3.0/layui.js" charset="utf-8"></script>
<script src="./js/main.js" charset="utf-8"></script>
<script src="./js/plugins/jquery-3.3.1.min.js" charset="utf-8"></script>
</body>
<script src="js/checkToken.js"></script>
<script type="text/javascript">
    $(document).keypress(function(e) {
        if((e.keyCode || e.which)==13) {
            // 触发需要调用的方法
            login();
        }
    });

    $.ajax({
        url:'http://localhost:8081/admin/login.do',
        headers:{
            accessToken:localStorage.getItem('accessToken')
        },
        success:function (data) {
            console.log(data.resMsg);
            if (data.resMsg=='用户已登录') {
                sessionStorage.setItem('userid',data.result.id);
                sessionStorage.setItem('username',data.result.name);
                sessionStorage.setItem('usertype',data.result.type);
                window.location.href = 'main.html';
            }
            if (data.resMsg=='token失效') {
                sessionStorage.clear();
                localStorage.clear();
            }
        }
    })


    // console.log(sessionStorage.getItem('userid'));
    // if (sessionStorage.getItem('userid')!=null)
    //     window.location.href='main.html';
    function login() {
        var name=$('#name').val();
        var password=$('#password').val();
        $('#unameW').empty();
        $('#upassW').empty();
        if (name==''){
            $('#unameW').append('<span style="color: #FD482C">用户名不能为空</span>')
            return;
        }
        if (password==''){
            $('#upassW').append('<span style="color: #FD482C">密码不能为空</span>')
            return;
        }
        $.ajax({
            url:'http://localhost:8081/admin/login.do?name='+name+'&password='+password,
            success:function (data) {
                if (data.resMsg=='登陆成功'){
                    sessionStorage.setItem('userid',data.result.id);
                    sessionStorage.setItem('username',data.result.name);
                    sessionStorage.setItem('usertype',data.result.type);
                    localStorage.setItem('accessToken',data.accessToken)
                    alert('登陆成功');
                    window.location.href='main.html';
                }else{
                    if(data.resMsg=='用户不存在')
                        $('#unameW').append('<span style="color: #FD482C">用户名不存在</span>')
                    else
                        $('#upassW').append('<span style="color: #FD482C">'+data.resMsg+'</span>')
                }
            }
        })
    }
</script>
</html>